//
//  TitleButton.swift
//  VVbo
//
//  Created by Mr.Run on 2017/8/25.
//  Copyright © 2017年 Mr.Run. All rights reserved.
//

import UIKit

class TitleButton: UIButton {

    init(title:String?) {
        super.init(frame: CGRect())
        
        if title == nil {
            setTitle("首页", for: [])
        }else{
            setTitle(title! + " ", for: [])
            setImage(UIImage(named:"navigationbar_arrow_down"), for: [])
            setImage(UIImage(named:"navigationbar_arrow_up"), for: .selected)
        }
        
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: [])
        sizeToFit()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        guard let titleLabel = titleLabel,
            let imageView = imageView
        else {
            return
        }
//        let titleLabelW = titleLabel.bounds.width
//        let imageViewW = imageView.bounds.width
        
        titleLabel.frame.origin.x = 0
        imageView.frame.origin.x = titleLabel.bounds.width
        
//        if titleLabel.frame.origin.x == 0{
//            return
//        }
//        titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageViewW, dy: 0)
//        imageView.frame = imageView.frame.offsetBy(dx: titleLabelW, dy: 0)
        
//        titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageView.bounds.width, dy: 0)
//        imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.width, dy: 0)
    }
}
